<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Excel向量化工具</title>
    <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/remixicon@2.5.0/fonts/remixicon.css">
    <style>
        :root {
            --primary-color: #4361ee;
            --primary-dark: #3a56d4;
            --secondary-color: #7209b7;
            --accent-color: #f72585;
            --bg-color: #f8f9fd;
            --card-bg: #ffffff;
            --text-primary: #2d3748;
            --text-secondary: #4a5568;
            --text-light: #718096;
            --border-color: #e2e8f0;
            --shadow-sm: 0 2px 4px rgba(0,0,0,0.05);
            --shadow-md: 0 4px 6px rgba(0,0,0,0.1);
            --shadow-lg: 0 10px 15px rgba(0,0,0,0.1);
            --radius-sm: 6px;
            --radius-md: 10px;
            --radius-lg: 16px;
            --transition: all 0.3s ease;
        }
        
        * {
            margin: 0;
            padding: 0;
            box-sizing: border-box;
            font-family: "PingFang SC", "Microsoft YaHei", sans-serif;
        }
        
        body {
            background-color: var(--bg-color);
            color: var(--text-primary);
            line-height: 1.6;
        }
        
        .container {
            max-width: 1000px;
            margin: 0 auto;
            padding: 20px;
        }
        
        .header {
            text-align: center;
            margin-bottom: 30px;
            position: relative;
        }
        
        .header::after {
            content: "";
            position: absolute;
            bottom: -10px;
            left: 50%;
            transform: translateX(-50%);
            width: 60px;
            height: 3px;
            background: linear-gradient(90deg, var(--primary-color), var(--secondary-color));
            border-radius: 3px;
        }
        
        .header h1 {
            color: var(--primary-color);
            font-size: 28px;
        }
        
        .header p {
            color: var(--text-light);
            margin-top: 5px;
        }
        
        .nav-links {
            display: flex;
            justify-content: center;
            margin-bottom: 30px;
            gap: 20px;
        }
        
        .nav-link {
            text-decoration: none;
            padding: 8px 16px;
            color: var(--text-primary);
            font-weight: 600;
            border-radius: var(--radius-sm);
            transition: var(--transition);
            display: flex;
            align-items: center;
            gap: 6px;
        }
        
        .nav-link:hover {
            background-color: rgba(67, 97, 238, 0.1);
            color: var(--primary-color);
            transform: translateY(-2px);
        }
        
        .nav-link i {
            font-size: 1.2em;
        }
        
        .card {
            background-color: var(--card-bg);
            border-radius: var(--radius-lg);
            box-shadow: var(--shadow-lg);
            padding: 30px;
            margin-bottom: 30px;
            position: relative;
            overflow: hidden;
        }
        
        .card-title {
            font-size: 20px;
            margin-bottom: 20px;
            color: var(--primary-color);
            position: relative;
            padding-left: 16px;
            display: flex;
            align-items: center;
            gap: 8px;
        }
        
        .card-title::before {
            content: "";
            position: absolute;
            left: 0;
            height: 100%;
            width: 4px;
            background: linear-gradient(to bottom, var(--primary-color), var(--secondary-color));
            border-radius: 2px;
        }
        
        .upload-area {
            border: 2px dashed var(--border-color);
            border-radius: var(--radius-md);
            padding: 30px;
            text-align: center;
            margin-bottom: 20px;
            transition: var(--transition);
            cursor: pointer;
        }
        
        .upload-area:hover {
            border-color: var(--primary-color);
            background-color: rgba(67, 97, 238, 0.05);
        }
        
        .upload-icon {
            font-size: 48px;
            color: var(--primary-color);
            margin-bottom: 15px;
        }
        
        .upload-text {
            color: var(--text-secondary);
            margin-bottom: 10px;
        }
        
        .upload-hint {
            color: var(--text-light);
            font-size: 14px;
        }
        
        .file-input {
            display: none;
        }
        
        .button {
            padding: 10px 20px;
            border: none;
            border-radius: var(--radius-md);
            background-color: var(--primary-color);
            color: white;
            font-weight: 600;
            cursor: pointer;
            transition: var(--transition);
            display: inline-flex;
            align-items: center;
            gap: 8px;
        }
        
        .button:hover {
            background-color: var(--primary-dark);
            transform: translateY(-2px);
            box-shadow: var(--shadow-md);
        }
        
        .button-secondary {
            background-color: #f3f4f6;
            color: var(--text-secondary);
        }
        
        .button-secondary:hover {
            background-color: #e5e7eb;
        }
        
        .button-danger {
            background-color: #f43f5e;
        }
        
        .button-danger:hover {
            background-color: #e11d48;
        }
        
        .button-group {
            display: flex;
            justify-content: flex-end;
            gap: 10px;
        }
        
        .alert {
            padding: 15px;
            border-radius: var(--radius-md);
            margin-bottom: 20px;
            display: none;
        }
        
        .alert-success {
            background-color: #d1fae5;
            color: #065f46;
            border-left: 4px solid #10b981;
        }
        
        .alert-error {
            background-color: #fee2e2;
            color: #b91c1c;
            border-left: 4px solid #ef4444;
        }
        
        .progress-container {
            margin-top: 20px;
            display: none;
        }
        
        .progress-bar {
            height: 6px;
            background-color: #e5e7eb;
            border-radius: 3px;
            overflow: hidden;
            margin-bottom: 10px;
        }
        
        .progress {
            height: 100%;
            background: linear-gradient(90deg, var(--primary-color), var(--secondary-color));
            border-radius: 3px;
            width: 0%;
            transition: width 0.3s ease;
        }
        
        .progress-text {
            font-size: 14px;
            color: var(--text-light);
            text-align: center;
        }
        
        .selected-file {
            padding: 10px;
            background-color: #f3f4f6;
            border-radius: var(--radius-md);
            margin-top: 10px;
            display: none;
            align-items: center;
            gap: 10px;
        }
        
        .file-icon {
            font-size: 20px;
            color: var(--primary-color);
        }
        
        .file-name {
            flex: 1;
            font-size: 14px;
            color: var(--text-secondary);
            white-space: nowrap;
            overflow: hidden;
            text-overflow: ellipsis;
        }
        
        .file-size {
            font-size: 12px;
            color: var(--text-light);
        }
        
        .remove-file {
            color: #ef4444;
            cursor: pointer;
            font-size: 18px;
        }
        
        .options-section {
            margin-top: 20px;
            padding-top: 20px;
            border-top: 1px solid var(--border-color);
        }
        
        .option-group {
            margin-bottom: 15px;
        }
        
        .option-group h3 {
            font-size: 16px;
            margin-bottom: 10px;
            color: var(--text-secondary);
        }
        
        .checkbox-group {
            display: flex;
            flex-wrap: wrap;
            gap: 15px;
        }
        
        .checkbox-wrapper {
            display: flex;
            align-items: center;
            gap: 8px;
        }
        
        .checkbox-wrapper input[type="checkbox"] {
            width: 18px;
            height: 18px;
            border-radius: 4px;
            border: 2px solid var(--primary-color);
            appearance: none;
            -webkit-appearance: none;
            outline: none;
            cursor: pointer;
            position: relative;
        }
        
        .checkbox-wrapper input[type="checkbox"]:checked {
            background-color: var(--primary-color);
        }
        
        .checkbox-wrapper input[type="checkbox"]:checked::after {
            content: "✓";
            position: absolute;
            color: white;
            font-size: 14px;
            top: 50%;
            left: 50%;
            transform: translate(-50%, -50%);
        }
        
        .checkbox-wrapper label {
            color: var(--text-secondary);
            cursor: pointer;
        }
        
        .range-wrapper {
            margin-top: 10px;
        }
        
        .range-wrapper label {
            display: block;
            margin-bottom: 8px;
            color: var(--text-secondary);
        }
        
        .range-controls {
            display: flex;
            align-items: center;
            gap: 15px;
        }
        
        .range-slider {
            flex: 1;
            -webkit-appearance: none;
            width: 100%;
            height: 6px;
            border-radius: 3px;
            background: #e5e7eb;
            outline: none;
        }
        
        .range-slider::-webkit-slider-thumb {
            -webkit-appearance: none;
            appearance: none;
            width: 18px;
            height: 18px;
            border-radius: 50%;
            background: var(--primary-color);
            cursor: pointer;
            transition: var(--transition);
        }
        
        .range-slider::-webkit-slider-thumb:hover {
            transform: scale(1.2);
        }
        
        .range-value {
            min-width: 40px;
            text-align: center;
            padding: 4px 8px;
            background-color: var(--primary-color);
            color: white;
            border-radius: var(--radius-sm);
            font-weight: 600;
        }
        
        @media (max-width: 768px) {
            .container {
                padding: 15px;
            }
            
            .card {
                padding: 20px;
            }
            
            .upload-area {
                padding: 20px;
            }
            
            .button-group {
                flex-direction: column;
            }
            
            .button {
                width: 100%;
                justify-content: center;
            }
        }
    </style>
</head>
<body>
    <div class="container">
        <div class="header">
            <h1>Excel向量化工具</h1>
            <p>将Excel文件内容转换为向量数据，支持智能问答</p>
        </div>
        
        <div class="nav-links">
            <a href="index.html" class="nav-link">
                <i class="ri-chat-3-line"></i>
                返回聊天
            </a>
            <a href="knowledge.html" class="nav-link">
                <i class="ri-book-2-line"></i>
                知识库管理
            </a>
        </div>
        
        <div class="card">
            <h2 class="card-title">
                <i class="ri-file-excel-2-line"></i>
                上传Excel文件
            </h2>
            
            <div id="alert-success" class="alert alert-success">
                <i class="ri-checkbox-circle-line"></i>
                <span id="success-message"></span>
            </div>
            
            <div id="alert-error" class="alert alert-error">
                <i class="ri-error-warning-line"></i>
                <span id="error-message"></span>
            </div>
            
            <div id="upload-area" class="upload-area">
                <i class="ri-upload-cloud-2-line upload-icon"></i>
                <p class="upload-text">点击或拖拽Excel文件到此处</p>
                <p class="upload-hint">支持 .xlsx 和 .xls 格式</p>
                <input type="file" id="file-input" class="file-input" accept=".xlsx, .xls">
            </div>
            
            <div id="selected-file" class="selected-file">
                <i class="ri-file-excel-2-line file-icon"></i>
                <span id="file-name" class="file-name"></span>
                <span id="file-size" class="file-size"></span>
                <i class="ri-close-circle-line remove-file" id="remove-file"></i>
            </div>
            
            <div class="options-section">
                <h2 class="card-title">
                    <i class="ri-settings-3-line"></i>
                    数据处理选项
                </h2>
                
                <div class="option-group">
                    <h3>数据清洗</h3>
                    <div class="checkbox-group">
                        <div class="checkbox-wrapper">
                            <input type="checkbox" id="clean-data" checked>
                            <label for="clean-data">启用数据清洗</label>
                        </div>
                        
                        <div class="checkbox-wrapper">
                            <input type="checkbox" id="skip-empty-rows" checked>
                            <label for="skip-empty-rows">跳过空行</label>
                        </div>
                        
                        <div class="checkbox-wrapper">
                            <input type="checkbox" id="remove-special-chars" checked>
                            <label for="remove-special-chars">清除特殊字符</label>
                        </div>
                        
                        <div class="checkbox-wrapper">
                            <input type="checkbox" id="remove-html-tags" checked>
                            <label for="remove-html-tags">清除HTML标签</label>
                        </div>
                    </div>
                </div>
                
                <div class="option-group">
                    <h3>内容过滤</h3>
                    <div class="range-wrapper">
                        <label for="min-content-length">最小有效内容长度</label>
                        <div class="range-controls">
                            <input type="range" id="min-content-length" class="range-slider" min="1" max="20" value="3">
                            <span id="min-content-length-value" class="range-value">3</span>
                        </div>
                    </div>
                </div>
            </div>
            
            <div class="progress-container" id="progress-container">
                <div class="progress-bar">
                    <div class="progress" id="progress"></div>
                </div>
                <p class="progress-text" id="progress-text">正在处理文件...</p>
            </div>
            
            <div class="button-group">
                <button id="cancel-button" class="button button-secondary">
                    <i class="ri-close-line"></i>
                    取消
                </button>
                <button id="upload-button" class="button">
                    <i class="ri-upload-2-line"></i>
                    开始处理
                </button>
            </div>
        </div>
    </div>
    
    <script>
        // DOM 元素
        const fileInput = document.getElementById('file-input');
        const uploadArea = document.getElementById('upload-area');
        const selectedFile = document.getElementById('selected-file');
        const fileName = document.getElementById('file-name');
        const fileSize = document.getElementById('file-size');
        const removeFile = document.getElementById('remove-file');
        const uploadButton = document.getElementById('upload-button');
        const cancelButton = document.getElementById('cancel-button');
        const progressContainer = document.getElementById('progress-container');
        const progress = document.getElementById('progress');
        const progressText = document.getElementById('progress-text');
        const alertSuccess = document.getElementById('alert-success');
        const alertError = document.getElementById('alert-error');
        const successMessage = document.getElementById('success-message');
        const errorMessage = document.getElementById('error-message');
        
        // 数据处理选项元素
        const cleanData = document.getElementById('clean-data');
        const skipEmptyRows = document.getElementById('skip-empty-rows');
        const removeSpecialChars = document.getElementById('remove-special-chars');
        const removeHtmlTags = document.getElementById('remove-html-tags');
        const minContentLength = document.getElementById('min-content-length');
        const minContentLengthValue = document.getElementById('min-content-length-value');
        
        // 当前选择的文件
        let currentFile = null;
        
        // 事件监听
        uploadArea.addEventListener('click', () => fileInput.click());
        uploadArea.addEventListener('dragover', handleDragOver);
        uploadArea.addEventListener('dragleave', handleDragLeave);
        uploadArea.addEventListener('drop', handleDrop);
        fileInput.addEventListener('change', handleFileSelect);
        removeFile.addEventListener('click', clearFileSelection);
        uploadButton.addEventListener('click', uploadExcelFile);
        cancelButton.addEventListener('click', resetForm);
        
        // 范围滑块监听
        minContentLength.addEventListener('input', function() {
            minContentLengthValue.textContent = this.value;
        });
        
        // 处理拖拽事件
        function handleDragOver(e) {
            e.preventDefault();
            e.stopPropagation();
            uploadArea.style.borderColor = 'var(--primary-color)';
            uploadArea.style.backgroundColor = 'rgba(67, 97, 238, 0.05)';
        }
        
        function handleDragLeave(e) {
            e.preventDefault();
            e.stopPropagation();
            uploadArea.style.borderColor = 'var(--border-color)';
            uploadArea.style.backgroundColor = '';
        }
        
        function handleDrop(e) {
            e.preventDefault();
            e.stopPropagation();
            uploadArea.style.borderColor = 'var(--border-color)';
            uploadArea.style.backgroundColor = '';
            
            const files = e.dataTransfer.files;
            if (files.length) {
                handleFiles(files);
            }
        }
        
        // 处理文件选择
        function handleFileSelect(e) {
            const files = e.target.files;
            if (files.length) {
                handleFiles(files);
            }
        }
        
        function handleFiles(files) {
            const file = files[0];
            const fileType = file.name.split('.').pop().toLowerCase();
            
            // 检查文件类型
            if (fileType !== 'xlsx' && fileType !== 'xls') {
                showError('请上传有效的Excel文件 (.xlsx 或 .xls)');
                return;
            }
            
            // 更新UI显示选中的文件
            currentFile = file;
            fileName.textContent = file.name;
            fileSize.textContent = formatFileSize(file.size);
            selectedFile.style.display = 'flex';
            
            // 隐藏之前的提示信息
            hideAlerts();
        }
        
        // 格式化文件大小
        function formatFileSize(bytes) {
            if (bytes === 0) return '0 Bytes';
            const k = 1024;
            const sizes = ['Bytes', 'KB', 'MB', 'GB'];
            const i = Math.floor(Math.log(bytes) / Math.log(k));
            return parseFloat((bytes / Math.pow(k, i)).toFixed(2)) + ' ' + sizes[i];
        }
        
        // 清除文件选择
        function clearFileSelection(e) {
            e.stopPropagation();
            currentFile = null;
            fileInput.value = '';
            selectedFile.style.display = 'none';
        }
        
        // 上传Excel文件
        function uploadExcelFile() {
            if (!currentFile) {
                showError('请先选择要上传的Excel文件');
                return;
            }
            
            // 获取数据处理选项
            const options = {
                cleanData: cleanData.checked,
                skipEmptyRows: skipEmptyRows.checked,
                minContentLength: parseInt(minContentLength.value),
                removeSpecialChars: removeSpecialChars.checked,
                removeHtmlTags: removeHtmlTags.checked
            };
            
            // 显示进度条
            progressContainer.style.display = 'block';
            progress.style.width = '0%';
            progressText.textContent = '正在处理文件...';
            
            // 创建FormData对象
            const formData = new FormData();
            formData.append('file', currentFile);
            formData.append('cleanData', options.cleanData);
            formData.append('skipEmptyRows', options.skipEmptyRows);
            formData.append('minContentLength', options.minContentLength);
            
            // 发送Ajax请求
            const xhr = new XMLHttpRequest();
            xhr.open('POST', '/api/excel/upload', true);
            
            // 处理上传进度
            xhr.upload.onprogress = function(e) {
                if (e.lengthComputable) {
                    const percentComplete = Math.round((e.loaded / e.total) * 100);
                    progress.style.width = percentComplete + '%';
                    progressText.textContent = `上传中... ${percentComplete}%`;
                }
            };
            
            // 请求完成回调
            xhr.onload = function() {
                try {
                    const response = JSON.parse(xhr.responseText);
                    
                    if (xhr.status === 200 && response.success) {
                        // 成功处理
                        progress.style.width = '100%';
                        progressText.textContent = '处理完成!';
                        
                        setTimeout(() => {
                            progressContainer.style.display = 'none';
                            clearFileSelection({ stopPropagation: () => {} });
                            showSuccess(response.message);
                        }, 1000);
                    } else {
                        // 处理失败
                        progressContainer.style.display = 'none';
                        showError(response.message || '文件处理失败，请重试');
                    }
                } catch (e) {
                    progressContainer.style.display = 'none';
                    showError('服务器响应无效，请稍后重试');
                }
            };
            
            // 请求错误回调
            xhr.onerror = function() {
                progressContainer.style.display = 'none';
                showError('网络错误，请检查您的连接并重试');
            };
            
            // 发送请求
            xhr.send(formData);
        }
        
        // 重置表单
        function resetForm() {
            currentFile = null;
            fileInput.value = '';
            selectedFile.style.display = 'none';
            progressContainer.style.display = 'none';
            hideAlerts();
        }
        
        // 显示成功提示
        function showSuccess(message) {
            alertSuccess.style.display = 'block';
            successMessage.textContent = message;
            alertError.style.display = 'none';
        }
        
        // 显示错误提示
        function showError(message) {
            alertError.style.display = 'block';
            errorMessage.textContent = message;
            alertSuccess.style.display = 'none';
        }
        
        // 隐藏所有提示
        function hideAlerts() {
            alertSuccess.style.display = 'none';
            alertError.style.display = 'none';
        }
    </script>
</body>
</html> 